package com.onlyphoto.onlymall.mvp.view.activity

import android.text.TextUtils
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentStatePagerAdapter
import com.onlyphoto.onlymall.R
import com.onlyphoto.onlymall.base.BaseActivity
import com.onlyphoto.onlymall.bean.response.TabBean
import com.onlyphoto.onlymall.mvp.contract.COrdersManager
import com.onlyphoto.onlymall.mvp.presenter.POrdersManagerImpl
import com.onlyphoto.onlymall.mvp.view.fragment.OrdersFragment
import com.onlyphoto.onlymall.utils.CollectionUtils
import kotlinx.android.synthetic.main.activity_ordermanager.*
import kotlinx.android.synthetic.main.activity_ordermanager.back


class OrderManagerActivity : BaseActivity<POrdersManagerImpl>(), COrdersManager.IVOrderManager {

    private var titles: MutableList<TabBean>? = null

    private var lastTitles: MutableList<TabBean>? = null

    private var names = arrayListOf("全部", "待付款", "待发货", "待收货", "退货订单")

    var status = 0

    override fun retry() {}

    override fun loadData() {
        status = intent.getIntExtra("status", 0)
        //加载数据
        val list = arrayListOf<TabBean>()
        for ((index, name) in names.withIndex()) {
            var bean: TabBean? = null
            if (index in 0..3) {
                bean = TabBean(index.toString(), name)
            }
            if (index == 4) {
                bean = TabBean("9", name)
            }
            bean?.let { list.add(it) }
        }
        getTagList(list)
        vp_container.currentItem = status
    }

    /**
     *比较两个list 内容是否一样
     */
    private fun isListEqual(l0: List<*>?, l1: List<*>?): Boolean {
        if (l0 === l1)
            return true
        if (l0 == null && l1 == null)
            return true
        if (l0 == null || l1 == null)
            return false
        if (l0.size != l1.size)
            return false
        for (o in l0) {
            if (!l1.contains(o))
                return false
        }
        for (o in l1) {
            if (!l0.contains(o))
                return false
        }
        return true
    }

    private fun getTabIndex(key: String): Int {
        for ((index, value) in titles!!.withIndex()) {
            if (TextUtils.equals(key, value.key)) {
                return index
            }
        }
        return 0
    }

    override fun init() {
        tl_tab.setupWithViewPager(vp_container)
        vp_container.offscreenPageLimit = 1
        vp_container.currentItem = 0
    }

    private fun getTagList(tags: MutableList<TabBean>) {
        if (CollectionUtils.isEmpty(lastTitles) || !isListEqual(lastTitles, tags)) {
            titles = tags
            vp_container.adapter = FragmentAdapter(this.supportFragmentManager, titles!!)
            lastTitles = titles
        }
    }

    override fun initListener() {
        //点击事件初始化
        back.setOnClickListener{
            finish()
        }
    }

    override fun isFullScreen(): Boolean {
        //是否沉浸式， 默认非沉浸式
        return false
    }

    public override fun initCustomerStatusManager() {

    }

    override fun setContentViewId(): Int {
        return R.layout.activity_ordermanager
    }

    override fun createPresenter() {
        mPresenter = POrdersManagerImpl(mContext, this)
    }

    override fun showLoading() {

    }

    override fun hideLoading() {

    }

    private class FragmentAdapter(fm: FragmentManager, val titleArray: MutableList<TabBean>) : FragmentStatePagerAdapter(fm) {

        override fun getItem(position: Int): Fragment {
            val bean = titleArray[position]
            val fragment = OrdersFragment.newInstance(bean.key)
            fragment.userVisibleHint = true
            return fragment
        }

        override fun getCount(): Int = titleArray.size

        override fun getPageTitle(position: Int): CharSequence {
            return titleArray[position].value
        }
    }

}
